<?php
/**
 * HtAccess system module.
 *
 * @author Martin "DevelX" Jurča
 */
class HtAccess extends Module {
    protected $name = 'HtAccess';
    protected $version = '2010-01-03';
    protected $system = true;
    protected $details = array('dependencies' => array(),
            'observes' => array('Modules'));

    public function install() {
        if (!file_exists(Config::ROOT . '/.htaccess')) {
            FTP::createFile('/.htaccess');
        }
        if (!is_writable(Config::ROOT . '/.htaccess')) {
            FTP::chmod('/.htaccess');
        }
        Modules::getModule('Users')->registerRights($this->name,
                $this->getRights());
        return $this->generateHtAccess(true);
    }

    public function uninstall() {
        FTP::deleteFile('/.htaccess');
        return !file_exists(Config::ROOT . '/.htaccess');
    }

    public function &onModuleInstalled(&$details, $source = '') {
        if ($source != 'Modules') {
            $result = false;
            return $result;
        }
        $result = $this->generateHtAccess();
        return $result;
    }

    public function &onModuleUninstalled(&$details, $source = '') {
        if ($source != 'Modules') {
            $result = false;
            return $result;
        }
        $result = $this->generateHtAccess();
        return $result;
    }

    /**
     * Generates .htaccess file.
     *
     * @return boolean True on success, false otherwise.
     */
    private function generateHtAccess($noCheck = false) {
        if (!$noCheck && !Modules::getModule('Users')->checkRights($this->name,
                'generate .htaccess')) {
            return;
        }
        $file = @fopen(Config::ROOT . '/.htaccess', 'w');
        if (!is_resource($file)) {
            return false;
        }
        @fwrite($file, "RewriteEngine on\n");
        $modules = Modules::getModuleList();
        foreach ($modules as $module) {
            $mod = Modules::getModule($module);
            if ($mod !== false) {
                @fwrite($file, $mod->getHtaccess());
            }
        }
        @fwrite($file, $this->getUniversalRules());
        @fwrite($file, "RewriteCond %{http_host} ^" . Config::DOMAIN_NAME .
                " [nc]\n");
        @fwrite($file, "RewriteRule ^(.*)$ http://www." . Config::DOMAIN_NAME .
                "/$1 [r=301,nc]\n");
        @fclose($file);
        return true;
    }

    /**
     * Returns universal rules usable for parsing any url into parameters while
     * extracting esential info like runtime mode for Kernel, whether to use
     * AJAX, etc... Modules should use these rules unless much complicated
     * rules are really necessary.
     *
     * @return String .htaccess universal rules.
     */
    private function getUniversalRules() {
        $rules = '';
        $rules .= "RewriteRule ^admin$ " . Config::WEB_DIR .
                "/index.php?mode=admin [L,QSA]\n";
        $rules .= "RewriteRule ^admin/$ " . Config::WEB_DIR . "/index.php?" .
                "mode=admin [L,QSA]\n";
        $rules .= "RewriteRule ^admin/(.+)/$ " . Config::WEB_DIR .
                "/index.php?mode=admin&url=$1 [L,QSA]\n";
        $rules .= "RewriteRule ^event/admin/([^/]+)/$ " . Config::WEB_DIR .
                "/index.php?mode=event&inAdmin=true&event=$1 [L,QSA]\n";
        $rules .= "RewriteRule ^event/admin/([^/]+)/([^/]+)/$ " .
                Config::WEB_DIR . "/index.php?mode=event&inAdmin=true&event=" .
                "$1&source=$2 [L,QSA]\n";
        $rules .= "RewriteRule ^event/admin/([^/]+)/([^/]+)/([^/]+)/$ " .
                Config::WEB_DIR . "/index.php?mode=event&inAdmin=true&event=" .
                "$1&source=$2&target=$3 [L,QSA]\n";
        $rules .= "RewriteRule ^event/admin/([^/]+)/([^/]+)/([^/]+)/(.+)$ " .
                Config::WEB_DIR . "/index.php?mode=event&inAdmin=true&event=" .
                "$1&source=$2&target=$3&url=$4 [L,QSA]\n";
        $rules .= "RewriteRule ^event/([^/]+)/$ " . Config::WEB_DIR .
                "/index.php?mode=event&event=$1 [L,QSA]\n";
        $rules .= "RewriteRule ^event/([^/]+)/([^/]+)/$ " . Config::WEB_DIR .
                "/index.php?mode=event&event=$1&source=$2 [L,QSA]\n";
        $rules .= "RewriteRule ^event/([^/]+)/([^/]+)/([^/]+)/$ " .
                Config::WEB_DIR . "/index.php?mode=event&event=$1&source=$2&" .
                "target=$3 [L,QSA]\n";
        $rules .= "RewriteRule ^event/([^/]+)/([^/]+)/([^/]+)/(.+)$ " .
                Config::WEB_DIR . "/index.php?mode=event&event=$1&source=$2&" .
                "target=$3&url=$4 [L,QSA]\n";
        $rules .= "RewriteRule ^ajax/admin/([^/]+)/$ " . Config::WEB_DIR .
                "/index.php?mode=admin&ajax=1&section=$1 [L,QSA]\n";
        $rules .= "RewriteRule ^ajax/admin/([^/]+)/(.+)/$ " . Config::WEB_DIR .
                "/index.php?mode=admin&ajax=1&section=$1&url=$2 [L,QSA]\n";
        $rules .= "RewriteRule ^ajax/([^/]+)/$ " . Config::WEB_DIR .
                "/index.php?ajax=1&section=$1 [L,QSA]\n";
        $rules .= "RewriteRule ^ajax/([^/]+)/(.+)/$ " . Config::WEB_DIR .
                "/index.php?ajax=1&section=$1&url=$2 [L,QSA]\n";
        $rules .= "RewriteRule ^(.+)/$ " . Config::WEB_DIR . "/index.php?" .
                "url=$1 [L,QSA]\n";
        $rules .= "ErrorDocument 404 " . Config::WEB_DIR .
                "/index.php?url=404\n";
        $rules .= "ErrorDocument 403 " . Config::WEB_DIR .
                "/index.php?url=403\n";
        return $rules;
    }
}
?>
